package com.yanqu.road.dao.impl.palace;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.estatewar.palace.PalaceAideDismissData;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class PalaceAideDismissDataDaoImpl extends TempDataObjectDao<PalaceAideDismissData> {

    public Map<Long, Map<Long, PalaceAideDismissData>> getPalaceAideDismissDataMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_c_palace_aide_dismiss where activityId=?";
        Map<Long, Map<Long, PalaceAideDismissData>> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(sql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                PalaceAideDismissData data = parse(rs);
                if(!dataMap.containsKey(data.getUserId())){
                    dataMap.put(data.getUserId(), new ConcurrentHashMap<>());
                }
                dataMap.get(data.getUserId()).put(data.getDismissUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    @Override
    protected boolean addData(PalaceAideDismissData data) {
        PreparedStatement call = null;
        String sql = "insert into t_c_palace_aide_dismiss(activityId,userId,dismissUserId,dismissTime) values (?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getActivityId());
            call.setLong(i++, data.getUserId());
            call.setLong(i++, data.getDismissUserId());
            call.setLong(i++, data.getDismissTime());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(PalaceAideDismissData data) {
        PreparedStatement call = null;
        String sql = "update t_c_palace_aide_dismiss set dismissTime=? where activityId=? and userId=? and dismissUserId=?";
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getDismissTime());
            call.setInt(i++, data.getActivityId());
            call.setLong(i++, data.getUserId());
            call.setLong(i++, data.getDismissUserId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected PalaceAideDismissData parseData(ResultSet rs) throws SQLException {
        PalaceAideDismissData data = new PalaceAideDismissData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setDismissUserId(rs.getLong("dismissUserId"));
        data.setDismissTime(rs.getLong("dismissTime"));
        return data;
    }
}
